package com.sydx.controller;


import com.sydx.entity.Order;
import com.sydx.service.OrderService;
import com.sydx.utils.MapControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {

    private static final String LIST = "order/list";

    @Autowired
    private OrderService orderService;


    @PostMapping("/create") // 写入数据
    @ResponseBody
    public Map<String,Object> create(@RequestBody Order order){
        int result = orderService.create(order);
        if(result<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String,Object> delete(@PathVariable("id") Integer id){
        int result = orderService.delete(id);
        if(result<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(String ids){
        int result = orderService.delete(ids);
        if(result<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }


    @PostMapping("/query")
    @ResponseBody
    public Map<String,Object> query(@RequestBody Order order){
        List<Order> list = orderService.query(order);
        // 分页
        Integer count = orderService.count(order);
        return MapControl.getInstance().success().page(list,count).getMap();
    }

    @GetMapping("/list")
    public String list(){
        return LIST;
    }


}
